#include"view_login.h"
using namespace std;

int view_login::process(Json::Value val,int cli_fd)
{
	sem_wait(&sem);
	cout<<"view_login::process()"<<endl<<endl;
	

	MYSQL_RES *mp_res;
	MYSQL_ROW mp_row;
	//选择数据库       数据库指针  库名
	if(mysql_select_db(mpcon,"chat"))
	{
		cout<<"select fail!"<<endl;
		sem_post(&sem);
		return 0;
	}
   


	string name = val["name"].asString();
	string pw = val["pw"].asString();

	//查询name是否存在于usr表
	char cmd[100] = "select * from usr;";
	//strcat(cmd,name.c_str());
	//strcat(cmd,"';");

	//输入指令
	if(mysql_real_query(mpcon,cmd,strlen(cmd)))
	{
		cout<<"login select usr fail"<<endl;
		sem_post(&sem);
		return 0;
	}
	
	//接受返回值
	mp_res = mysql_store_result(mpcon);

	//接受返回值的一行

	while(mp_row = mysql_fetch_row(mp_res))
	{
		if(strcmp(mp_row[0],name.c_str())==0)
		{
			break;
		}
	}
	if(mp_row == 0)
	{
		cout<<name<<"not exist"<<endl;
		//回复用户
		responce(0,cli_fd,NULL);
		sem_post(&sem);
		mysql_free_result(mp_res);
		return 0;
	}

	if(strcmp(mp_row[1],pw.c_str()))
	{
		cout<<"pw not ok"<<endl;
		//回复用户

		responce(1,cli_fd,NULL);
		sem_post(&sem);
		mysql_free_result(mp_res);
		return 0;
	}
	
	cout<<cli_fd<<" login success"<<endl;
	
	
	
	//将用户加入在线表
    char cmd3[100]="insert into online(name,fd) values('";
	strcat(cmd3,name.c_str());
	strcat(cmd3,"',");

	char cli_fdstr[10];
	sprintf(cli_fdstr,"%d",cli_fd);
	strcat(cmd3,cli_fdstr);
	strcat(cmd3,");");
	if(mysql_real_query(mpcon,cmd3,strlen(cmd3)))
	{
		cout<<"add online fail::"<<cmd3<<endl;
		sem_post(&sem);
			mysql_free_result(mp_res);
		return 0;
	}

	//在离线表内查找是否存在用户，如果存在  ，给用户发送离线消息
	
	char cmd2[100] = "select * from offline where name='";
	strcat(cmd2,name.c_str());
	strcat(cmd2,"';");
	if(mysql_real_query(mpcon,cmd2,strlen(cmd2)))
	{
		cout<<"login select offline fail"<<endl;
		sem_post(&sem);
		mysql_free_result(mp_res);
		return 0;
	}
	mp_res = mysql_store_result(mpcon);
	

	
	
	if(mp_row=mysql_fetch_row(mp_res))
	{
		responce(3,cli_fd,NULL);
	    sleep(1);

		do
		{
		cout<<"send notes"<<mp_row[1]<<endl;
		send(cli_fd,mp_row[1],1024,0);
		
		}while(mp_row=mysql_fetch_row(mp_res));
		
		
		
		send(cli_fd,"##sendoffnewend",128,0);
		
		char cmd4[100] = "delete  from offline where name='";
		strcat(cmd4,name.c_str());
		 strcat(cmd4,"';");
		
		 if(mysql_real_query(mpcon,cmd4,strlen(cmd4)))
		{
			cout<<"delete offline failed!"<<endl;
			sem_post(&sem);
				mysql_free_result(mp_res);
			return 0;
		}
	    cout<<"send success!!"<<endl;

	       sem_post(&sem);
		   mysql_free_result(mp_res);
	   
	
	}
	
	
	
	else 
		
	{
		responce(2,cli_fd,NULL);
        sem_post(&sem);
		mysql_free_result(mp_res);
	}
}

void view_login::responce(int n,int cli_fd,void *arg)
{

	switch (n)
	{
	case 0:
		{
			int res=send(cli_fd,"##login_failed0",128,0);
			if(-1==res)
			{
			  cout<<"send ##login_failed0 failed!"<<endl;
	
			}
			
		}
		break;
	case 1:
		{
			int res=send(cli_fd,"##login_failed1",128,0);
			if(-1==res)
			{
			   cout<<"send ##login_failed1 failed!"<<endl;
	
			}
		}
		break;
	
	case 2:
		{
		int res=send(cli_fd,"##login_success0",128,0);
		if(-1==res)
		{
		cout<<"send ##login_success0 failed!"<<endl;
		}
		}
		break;
	case 3:
		{
			int res=send(cli_fd,"##login_success1",128,0);
         if(-1==res)
		 {
		  cout<<"send ##login_success1 failed"<<endl;
		 }

		}

		break;
	default:
		cout<< "view_login::responce  parameter error!"<<endl;
	}
	
}
